package cn.darkhorse.mesadmin.controller.pick;


import cn.darkhorse.pick.pojo.PickOrder;
import cn.darkhorse.pick.pojo.PickOrderPay;
import cn.darkhorse.pick.service.PickOrderPayService;
import cn.darkhorse.pick.service.PickOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangtianqi
 * @since 2025-07-08
 */
@RestController
@RequestMapping("/pick/pickOrder")
@Api(tags = "作业单管理")
public class PickOrderController extends BaseController {
    @Autowired
    private PickOrderService pickOrderService;
    @Autowired
    private PickOrderPayService pickOrderPayService;

    /**
     * 查询列表
     * @param pickOrder
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public TableDataInfo list(PickOrder pickOrder) {
        startPage();
        return getDataTable(pickOrderService.listAll(pickOrder));
    }


    /**
     * 查询详情
     */
    @GetMapping("/view")
    @ApiOperation(value = "详情")
    public AjaxResult view(Long id) {
        return success(pickOrderService.getById(id));
    }


    /**
     * 新增
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增")
    public AjaxResult add(@RequestBody PickOrder pickOrder) {
        String orderNumber = pickOrder.getOrderNumber();
        Long count = pickOrderService.lambdaQuery().eq(PickOrder::getOrderNumber, orderNumber).count();
        if (count>0){
            return AjaxResult.error("单号已存在");
        }
        if (pickOrder.getCollectPrice()!=null && pickOrder.getCollectArea()!=null){
            pickOrder.setCollectAmount(pickOrder.getCollectPrice().multiply(pickOrder.getCollectArea()));
        }
        if (pickOrder.getSecondaryPrice()!=null && pickOrder.getSecondaryArea()!=null){
            pickOrder.setSecondaryAmount(pickOrder.getSecondaryPrice().multiply(pickOrder.getSecondaryArea()));
        }
        pickOrderService.save(pickOrder);
        return success();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public AjaxResult update(@RequestBody PickOrder pickOrder) {
        pickOrderService.updateById(pickOrder);
        return success();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public AjaxResult delete(Long id) {
        pickOrderService.removeById(id);
        return success();
    }


    /**
     * 结算
     * @return
     */
    @PostMapping("/settlement")
    @ApiOperation(value = "结算")
    public AjaxResult settlement(Long id) {
        pickOrderService.lambdaUpdate().set(PickOrder::getStatus, 1).eq(PickOrder::getId, id).update();
        return success();
    }

    /**
     * 取消结算
     */
    @PostMapping("/cancel-settlement")
    @ApiOperation(value = "取消结算")
    public AjaxResult cancelSettlement(Long id) {
        PickOrder pickOrder = pickOrderService.getById(id);
        if (pickOrder.getPayStartTime()!=null){
            pickOrder.setPayStartTime( null);
            PickOrderPay orderPay = pickOrderPayService.lambdaQuery().eq(PickOrderPay::getOrderNumber, pickOrder.getOrderNumber())
                    .eq(PickOrderPay::getPayStatus, 0)
                    .one();
            if (orderPay != null){



                orderPay.setPayStatus(2);
                pickOrderPayService.updateById(orderPay);
            }
        }
        pickOrderService.lambdaUpdate().set(PickOrder::getStatus, 3).eq(PickOrder::getId, id).update();
        return success();
    }


    /**
     * 获取单号
     */
    @GetMapping("/getOrderNo")
    @ApiOperation(value = "获取单号")
    public AjaxResult getOrderNo() {
        return success(pickOrderService.getOrderNo());
    }


    @PostMapping("/getFarmerNameByCode")
    @ApiOperation(value = "获取农户")
    public AjaxResult getFarmerNameByCode(String code) {
        List<PickOrder> pickOrders = pickOrderService.lambdaQuery().eq(PickOrder::getFarmersNumber, code).list();
        if (!pickOrders.isEmpty()){
            return success(pickOrders.get(0).getFarmersName());
        }
        return error();
    }



}

